/*
 * @Author: 小豪
 * @Description:
 * @Date: 2024-04-29 14:36:04
 * @LastEditTime: 2024-07-24 16:04:10
 * @FilePath: \mddj\gohub\pkg\str\str.go
 */
// Package str 字符串辅助方法
package str

import (
	"fmt"
	"os"
	"strings"

	"github.com/gertd/go-pluralize"
	"github.com/iancoleman/strcase"
)

// Plural 转为复数 user -> users
func Plural(word string) string {
	return pluralize.NewClient().Plural(word)
}

// Singular 转为单数 users -> user
func Singular(word string) string {
	return pluralize.NewClient().Singular(word)
}

// Snake 转为 snake_case，如 TopicComment -> topic_comment
func Snake(s string) string {
	return strcase.ToSnake(s)
}

// Camel 转为 CamelCase，如 topic_comment -> TopicComment
func Camel(s string) string {
	return strcase.ToCamel(s)
}

// LowerCamel 转为 lowerCamelCase，如 TopicComment -> topicComment
func LowerCamel(s string) string {
	return strcase.ToLowerCamel(s)
}
func StrFirstToUpper(str string) string {
	if len(str) < 1 {
		return ""
	}

	strArray := []rune(str)

	if strArray[0] >= 97 && strArray[0] <= 122 {
		strArray[0] -= 32
	}

	return string(strArray)
}

// 将下划线连接的字串改为驼峰
// abc_def --> AbcDef
func StrCamel(str string) string {
	tmp := strings.Split(str, "_")
	var result string
	for _, v := range tmp {
		result += StrFirstToUpper(v)
	}

	return result
}

// 判断文件或路径是否存在
func PathExists(path string) bool {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return false
	}

	return true
}

// 获取tpl所在目录
func GetTplPath() string {
	dir, _ := os.Getwd()

	return fmt.Sprintf("%s../tpl", dir)
}
